package com.sduept.nwld.wave.util;

import com.sduept.nwld.dataserver.util.CimUtils;
import com.sduept.stardfile.parser.tool.NameTool;
import lombok.extern.slf4j.Slf4j;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class CfgChnsParserUtil {
	public static final int TYPE_BREAKER = 1;//断路器
	public static final int TYPE_POWER = 2;//变压器
	public static final int TYPE_BUSBAR = 3;//母线
	public static final int TYPE_LINE = 4;//交流线
	public static final int TYPE_HYDRO = 5;//发电机
	public static final int TYPE_PROTECT = 6;//保护
	public static final int TYPE_COMPEN =7;//电抗器  
	public static final int TYPE_PROTECT_BREAKER=8;//断路器保护
	public static final int TYPE_PROTECT_POWER=9;//变压器保护
	public static final int TYPE_PROTECT_LINE=10;//线路保护
	public static final int TYPE_PROTECT_BUSBAR=11;//母线保护
	public static final String regex_feature_breaker = "\\d{4}|\\d{3}";
	public static final String regex_feature_busbar = "\\d{1}";
	public static final Pattern BREAKER_NAME_PATTERN = Pattern.compile(regex_feature_breaker);
	public static final Pattern BUSBAR_NAME_PATTERN = Pattern.compile(regex_feature_busbar);
	public static int getAnalogType(String name){
		if (name.contains("断路器")||name.contains("开关")){
			return TYPE_BREAKER;
		}else if (name.contains("变")&&!name.contains("油温")&&!name.contains("温度")){
			return TYPE_POWER;
		}else if (name.contains("母")||name.toUpperCase().contains("M")){
			return TYPE_BUSBAR;
		}else if(name.contains("抗")){
			return TYPE_COMPEN;
		} else if(name.contains("线")||name.contains("回")){
			return TYPE_LINE;
		}else if(name.contains("发电机")||name.contains("电机")){
			return TYPE_HYDRO;
		} else {
			Pattern dlq = Pattern.compile(regex_feature_breaker);
			if(dlq.matcher(name).find()){
				return TYPE_BREAKER;
			}
		}
		return 0;
	}
	public static int getDigitalType(String name){
		if ((name.contains("断路器")&&!name.contains("主")||name.contains("开关")&&!name.contains("主"))){
			if(name.contains("位")&&(name.contains("分")||name.contains("跳")||name.contains("合"))){
				return TYPE_BREAKER;
			}else{
				return TYPE_PROTECT_BREAKER;
			}
		}else if (name.contains("变"))
			return TYPE_PROTECT_POWER;
		else if (name.contains("母线")||name.toUpperCase().contains("M"))
			return TYPE_PROTECT_BUSBAR;
		else if(name.contains("线")||name.contains("回")){
			return TYPE_PROTECT_LINE;
		}else {
			Pattern dlq = Pattern.compile(regex_feature_breaker);
			if(dlq.matcher(name).find()){
				//TODO 需要处理
				return TYPE_PROTECT_BREAKER;
			}
		}
		return 0;
	}
	public static String getWindsType(String name){
		if(name.contains("高")||name.contains("H")){
			return "H";
		}else if(name.contains("中")||name.contains("M")){
			return "M";
		}else if(name.contains("低")||name.contains("L")){
			return "L";
		}else{
			return name;
		}
	}
	 public static String convertString(String sourceStr, String[][] source_regex){
			String rtn = sourceStr;
			for( String[] et : source_regex){
				rtn = rtn.replaceAll(et[0],et[1]);
			}
			return rtn;
		}
	 public static String[][] line_regex = new String[][]{
			{"\\s*", ""},
			{"//|[0-9]+[-,_]", ""},
			{"//|[0-9]+kV", ""},
			{"//|回$", "线"},
			{"//|丁|IV|iv|Ⅳ|4", "四"},
			{"//|丙|III|iii|Ⅲ|3", "三"},
			{"//|乙|II|ii|Ⅱ|2", "二"},
			{"//|甲|I|i|Ⅰ|1", "一"}
		};
		public static String[][] power_regex = new String[][]{
			{"\\s*", ""},
			{"//|[0-9]+[-,_]", ""},
			{"//|[0-9]+[k,K]+[v,V]", ""},
			{"//|10|35|220|110|500|800", ""},
			{"//|III|iii|Ⅲ", "3"},
			{"//|II|ii|Ⅱ", "2"},
			{"//|I|i|Ⅰ|1", "1"}
		};
		public static String[][] break_regex = new String[][]{
			{"\\s*", ""},
			{"//|[0-9]{1,3}[-]", ""},
			{"//|[0-9]+[k,K]+[V,v]", ""}
		};
		public static String[][] bus_regex = new String[][]{
			{"\\s*", ""},
//			{"//|[0-9]{1,}[-]", ""},
//			{"//|10|35|220|110|500|800", ""},
			{"//|[k,K]+[v,V]", ""},
			{"//|VIII|viii|Ⅷ", "8"},
			{"//|VII|vii|Ⅶ", "7"},
			{"//|VI|vi|Ⅵ", "6"},
			{"//|V|v|Ⅴ", "5"},
			{"//|IV|iv|Ⅳ", "4"},
			{"//|III|iii|Ⅲ", "3"},
			{"//|II|ii|Ⅱ", "2"},
			{"//|I|i|Ⅰ", "1"}
		};
		
		public static String getLineNameOfNw(String lineName){
			int pos = lineName.indexOf("回线");
			if(pos > -1){
				lineName =  lineName.substring(0, pos+1);
			} else {
				pos = lineName.indexOf("线");
				if(pos > -1){
					lineName =  lineName.substring(0, pos+1);
				} else {
					 pos = lineName.indexOf("回");
					 if(pos > -1){
						 lineName = lineName.substring(0,  pos+1);
					 } 
				}
			}
			if(pos > -1)
				lineName = convertString(lineName,line_regex);
			return lineName;
		}
		public static String getBreakerNameOfNw(String name){
			Pattern first= Pattern.compile("\\d{4}");
			Matcher firstMatch = first.matcher(name);
			if (firstMatch.find()) {
				return firstMatch.group(0);
			}
			name = convertString(name,break_regex);
			Matcher m = BREAKER_NAME_PATTERN.matcher(name);
			if (m.find()) {
				return m.group(0);
			}
			
			int pos = name.indexOf("断路器");
			if(pos > -1){
				name = name.substring(0, pos+3);
			} else {
				pos = name.indexOf("开关");
				if(pos > -1){
					name = name.substring(0,  pos+2);
				} 
			}
			if(pos > -1)
				name = convertString(name,break_regex);
			return name;
		}
		public static String getPowerNameOfNw(String name){
			int pos = name.indexOf("变");
			if(pos>-1){
				name=name.substring(0,pos+1);
			}
			name = convertString(name,power_regex);
			name=name.replaceAll("[^.^\\d]", "");
			return name;
		}
		public static boolean isLineProtectionEquals(String protectionName,String statusChannelName){
			String dbPtype = ProtectionNwUtil.getProtectionType(protectionName);
			String cfgPtype = ProtectionNwUtil.getProtectionType(statusChannelName);
			String dbLineName = getLineNameOfNw(protectionName);
			String cfgLineName = getLineNameOfNw(statusChannelName);
			boolean le = CimUtils.lineNameMatching(dbLineName, cfgLineName);
			boolean te = dbPtype.equals(cfgPtype);
			return le&&te;
		}
		public static String getHydroName(String name){
			name=name.replaceAll("[0-9]{1,}[-]", "");
			int pos = name.indexOf("电机");
			if(pos>-1){
				name=name.substring(0, pos);
			}
			return name;
		}
		public static String getBusNameOfNw(String name,boolean isSign){
			if(isSign){
				name=name.replaceAll("[0-9]{1,}[-_]", "");
			}
			int pos = name.indexOf("母");
			if(pos > -1){
				name = name.substring(0, pos+1);
			} else {
				pos = name.indexOf("M");
				if(pos > -1){
					name = name.substring(0,  pos+1);
				} 
			}
			name = convertString(name,bus_regex);
			name=name.replaceAll("[^\\d]", "");
//			Matcher m = BUSBAR_NAME_PATTERN.matcher(name);
//			if (m.find()) {
//				return m.group(0);
//			}
			return name;
		}
		public static String getCompenName(String name){
			int pos = name.indexOf("抗");
			if(pos>-1){
				name=name.substring(0,pos-1);
				name=name.replaceAll("#", "");
				name=name.replaceAll("＃", "");
			}
			name=convertString(name, line_regex);
			return name;
		}
		 public static String getNumbers(String content) {  
		       Pattern pattern = Pattern.compile("\\d+");  
		       Matcher matcher = pattern.matcher(content);  
		       while (matcher.find()) { 
		           return matcher.group(0);  
		       }  
		       return "";  
		   }  
		 
	public static boolean isBusProtectionEquals(String protectionName,
			String statusChannelName) {
		if(NameTool.getBusNameOfNw(protectionName).equals(NameTool.getBusNameOfNw(statusChannelName))
				&&(ProtectionNwUtil.getProtectionType(protectionName).equals(ProtectionNwUtil.getProtectionType(statusChannelName))
						)){
			return true;
		}else{
			return false;
		}
		//return CimUtils.busNameMatching(protectionName,statusChannelName);
	}

	public static void main(String[] args) {
//		String s1="35kV #S1M 母线保护动作";
//		String s1Bus=NameTool.getBusNameOfNw(s1);
//		String s2="500kV #S1母线第一套母线保护(BP-2B)";
//		String s2Bus=NameTool.getBusNameOfNw(s2);
//		log.info(s1Bus);
//		log.info(s2Bus);
//		String type1 = ProtectionNwUtil.getProtectionType(s1);
//		String type2 = ProtectionNwUtil.getProtectionType(s2);
//		log.info(type1);
//		log.info(type2);
		
		String tmpname="1-5011断路器保护A相出口跳闸";
		String protectedName="500kV第一串5011断路器保护RCS921";
		if(tmpname.startsWith(""+1)){
			tmpname=tmpname.replaceFirst(""+1, "");
		}else if(tmpname.startsWith("0"+1)){
			tmpname=tmpname.replaceFirst("0"+1, "");
		}
		if(tmpname.length()>0&&protectedName.contains(CfgChnsParserUtil.getBreakerNameOfNw(tmpname))){
			log.info(getBreakerNameOfNw(protectedName));
		}
		
	}
}

